import Vue from "vue";
import Vuex from 'vuex'
Vue.use(Vuex)
const store = new Vuex.Store({
  // !严格模式  无论何时发生了状态变更且不是由 mutations 函数引起的，将会抛出错误
  strict: process.env.NODE_ENV === 'development',
  state: {
    money: 1000,
    car: '凤凰车'
  },
  mutations: {
    // 第一个参数为state
    addMoney(state) {
      state.money++
    },
    // 要么两个参数，超过两个参数，第二个参数用payload对象来接或者给payload解构
    addMoreMoney(state, money) {
      state.money += money
    },
    buyCar(state, { car, val }) {
      state.money -= val
      state.car = car
    },
    buyLip(state, money) {
      state.money -= money
    }
  },
  actions: {
    buyLipAsync(context, money) {
      setTimeout(() => {
        context.commit('buyLip', money)
      }, 1000)

    }
  }
})
export default store